* Build patient level dataset - step one

forval year==2005(1)2010{
local z = `year'+1

use "$savedata/ami_`year'", clear

keep extract pconsult procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* epiorder admimeth gpprac

duplicates drop
keep if ami==1
keep if finyear==`year'

merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==2

* We want to keep the first episode where they are treated by a cardiologist - this will be the identify of the consultant we care about
drop if admimeth>80

* Take the first 4 episodes
foreach x in 1 2 3 4{
gen cardio`x' = 0
replace cardio`x' = 1 if epiorder==`x' & _merge==3
}

gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)

foreach x in 1 2 3 4{
egen cardio_order`x' = max(cardio`x'), by(extract admidate_f)
}

gen first_cardio = 0
replace first_cardio = 1 if cardio_order1==1
replace first_cardio = 2 if cardio_order2==1 & cardio_order1==0
replace first_cardio = 3 if cardio_order3==1 & cardio_order2==0 & cardio_order1==0
replace first_cardio = 4 if cardio_order4==1 & cardio_order3==0 & cardio_order2==0 & cardio_order1==0

sort extract admidate_f epiorder

gen cons_before_cardio = . if first_cardio==0
replace cons_before_cardio = 0 if first_cardio==1
replace cons_before_cardio = 1 if first_cardio==2
replace cons_before_cardio = 2 if first_cardio==3
replace cons_before_cardio = 3 if first_cardio==4

gen first_pconsult=pconsult[_n-1] if first_cardio==2
replace first_pconsult = pconsult[_n-2] if first_cardio==3
replace first_pconsult = pconsult[_n-3] if first_cardio==4

gen first_tretspef=tretspef[_n-1] if first_cardio==2
replace first_tretspef = tretspef[_n-2] if first_cardio==3
replace first_tretspef = tretspef[_n-3] if first_cardio==4

* Keep the first episode where cardio_order is ==1, with the rest of the info required
gen keep_epi = first_cardio

*replace keep_epi = 1 if first_cardio==0
keep if epiorder==keep_epi

* Drop people who seem to appear multiple times on the same day
bys extract admidate_day admidate_month admidate_year: gen z=_n
egen zz=max(z), by(extract)
drop if zz>1

* Keep the first heart attack if someone has multiple heart attacks
sort extract admidate_day admidate_month admidate_year
egen first_admidate = min(admidate_f), by(extract)
keep if admidate_f == first_admidate

* Now I want to record deaths and readmissions
gen trust_code = substr(procode,1,3)
	
keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac

foreach type in year month day{
rename admidate_`type' ami_admidate_`type'
}

gen readmit_30=0
gen death_30=0

merge 1:m extract_hesid using "$rawdata/ip_`year'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	gen ami_date = mdy(ami_admidate_month, ami_admidate_day, ami_admidate_year)
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
		keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex ami_date soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* readmit_30 death_30 gpprac
		
	
	
merge 1:m extract_hesid using "$rawdata/ip_`z'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
	keep  extract pconsult procode sitetret trust_code finyear ami ethnos derv_age sex soal soam fulldiag3* tretspef mainspef ami_date readmit_30 death_30 cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac
	compress
	
	save "$savedata/ami_readmit_death_`year'_evercardio", replace
	
}


*2011 - 2012

forval year==2011(1)2012{
local z = `year'+1

use "$savedata/ami_`year'", clear

keep extract pconsult procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day lsoa msoa fulldiag3* tretspef mainspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* epiorder admimeth gpprac

rename lsoa soal
rename msoa soam

duplicates drop
keep if ami==1
keep if finyear==`year'

merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==2

* We want to keep the first episode where they are treated by a cardiologist - this will be the identify of the consultant we care about
drop if admimeth>80

* Take the first 4 episodes
foreach x in 1 2 3 4{
gen cardio`x' = 0
replace cardio`x' = 1 if epiorder==`x' & _merge==3
}

gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)

foreach x in 1 2 3 4{
egen cardio_order`x' = max(cardio`x'), by(extract admidate_f)
}

gen first_cardio = 0
replace first_cardio = 1 if cardio_order1==1
replace first_cardio = 2 if cardio_order2==1 & cardio_order1==0
replace first_cardio = 3 if cardio_order3==1 & cardio_order2==0 & cardio_order1==0
replace first_cardio = 4 if cardio_order4==1 & cardio_order3==0 & cardio_order2==0 & cardio_order1==0

sort extract admidate_f epiorder

gen cons_before_cardio = . if first_cardio==0
replace cons_before_cardio = 0 if first_cardio==1
replace cons_before_cardio = 1 if first_cardio==2
replace cons_before_cardio = 2 if first_cardio==3
replace cons_before_cardio = 3 if first_cardio==4

gen first_pconsult=pconsult[_n-1] if first_cardio==2
replace first_pconsult = pconsult[_n-2] if first_cardio==3
replace first_pconsult = pconsult[_n-3] if first_cardio==4

gen first_tretspef=tretspef[_n-1] if first_cardio==2
replace first_tretspef = tretspef[_n-2] if first_cardio==3
replace first_tretspef = tretspef[_n-3] if first_cardio==4

* Keep the first episode where cardio_order is ==1, with the rest of the info required
gen keep_epi = first_cardio

*replace keep_epi = 1 if first_cardio==0
keep if epiorder==keep_epi


* Drop people who seem to appear multiple times on the same day
bys extract admidate_day admidate_month admidate_year: gen z=_n
egen zz=max(z), by(extract)
drop if zz>1

* Keep the first heart attack if someone has multiple heart attacks
sort extract admidate_day admidate_month admidate_year
egen first_admidate = min(admidate_f), by(extract)
keep if admidate_f == first_admidate



* Now I want to record deaths and readmissions
gen trust_code = substr(procode,1,3)
	
keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac

foreach type in year month day{
rename admidate_`type' ami_admidate_`type'
}

gen readmit_30=0
gen death_30=0

merge 1:m extract_hesid using "$rawdata/ip_`year'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	gen ami_date = mdy(ami_admidate_month, ami_admidate_day, ami_admidate_year)
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
		keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex ami_date soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* readmit_30 death_30 gpprac
	
merge 1:m extract_hesid using "$rawdata/ip_`z'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
	keep  extract pconsult procode sitetret trust_code finyear ami ethnos derv_age sex soal soam fulldiag3* tretspef mainspef ami_date readmit_30 death_30 cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac
	compress
	
	save "$savedata/ami_readmit_death_`year'_evercardio", replace
	
}


*2013  - 2014
****************

forval year==2013(1)2014{
local z = `year'+1

use "$savedata/ami_`year'", clear

keep extract pconsult procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day lsoa msoa fulldiag3* tretspef mainspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* epiorder admimeth gpprac

rename lsoa soal
rename msoa soam

duplicates drop
keep if ami==1
keep if finyear==`year'

merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==2

* We want to keep the first episode where they are treated by a cardiologist - this will be the identify of the consultant we care about
drop if admimeth=="81" | admimeth=="83" | admimeth=="98" | admimeth=="99" 

* Take the first 4 episodes
foreach x in 1 2 3 4{
gen cardio`x' = 0
replace cardio`x' = 1 if epiorder==`x' & _merge==3
}

gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)

foreach x in 1 2 3 4{
egen cardio_order`x' = max(cardio`x'), by(extract admidate_f)
}

gen first_cardio = 0
replace first_cardio = 1 if cardio_order1==1
replace first_cardio = 2 if cardio_order2==1 & cardio_order1==0
replace first_cardio = 3 if cardio_order3==1 & cardio_order2==0 & cardio_order1==0
replace first_cardio = 4 if cardio_order4==1 & cardio_order3==0 & cardio_order2==0 & cardio_order1==0

sort extract admidate_f epiorder

gen cons_before_cardio = . if first_cardio==0
replace cons_before_cardio = 0 if first_cardio==1
replace cons_before_cardio = 1 if first_cardio==2
replace cons_before_cardio = 2 if first_cardio==3
replace cons_before_cardio = 3 if first_cardio==4

gen first_pconsult=pconsult[_n-1] if first_cardio==2
replace first_pconsult = pconsult[_n-2] if first_cardio==3
replace first_pconsult = pconsult[_n-3] if first_cardio==4

gen first_tretspef=tretspef[_n-1] if first_cardio==2
replace first_tretspef = tretspef[_n-2] if first_cardio==3
replace first_tretspef = tretspef[_n-3] if first_cardio==4

* Keep the first episode where cardio_order is ==1, with the rest of the info required
gen keep_epi = first_cardio

*replace keep_epi = 1 if first_cardio==0
keep if epiorder==keep_epi


* Drop people who seem to appear multiple times on the same day
bys extract admidate_day admidate_month admidate_year: gen z=_n
egen zz=max(z), by(extract)
drop if zz>1

* Keep the first heart attack if someone has multiple heart attacks
sort extract admidate_day admidate_month admidate_year
egen first_admidate = min(admidate_f), by(extract)
keep if admidate_f == first_admidate


* Now I want to record deaths and readmissions
gen trust_code = substr(procode,1,3)
	
keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac

foreach type in year month day{
rename admidate_`type' ami_admidate_`type'
}

gen readmit_30=0
gen death_30=0

merge 1:m extract_hesid using "$rawdata/ip_`year'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	gen ami_date = mdy(ami_admidate_month, ami_admidate_day, ami_admidate_year)
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
		keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex ami_date soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* readmit_30 death_30  gpprac
	
merge 1:m extract_hesid using "$rawdata/ip_`z'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
	keep  extract pconsult procode sitetret trust_code finyear ami ethnos derv_age sex soal soam fulldiag3* tretspef mainspef ami_date readmit_30 death_30 cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac
	compress
	
	save "$savedata/ami_readmit_death_`year'_evercardio", replace
	
}


*2015
****************

forval year==2015(1)2015{
local z = `year'+1

use "$savedata/ami_`year'", clear

keep extract pconsult procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day lsoa msoa fulldiag3* tretspef mainspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* epiorder admimeth gpprac

rename lsoa soal
rename msoa soam

duplicates drop
keep if ami==1
keep if finyear==`year'

merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==2

* We want to keep the first episode where they are treated by a cardiologist - this will be the identify of the consultant we care about
drop if admimeth=="81" | admimeth=="83" | admimeth=="98" | admimeth=="99" 

* Take the first 4 episodes
foreach x in 1 2 3 4{
gen cardio`x' = 0
replace cardio`x' = 1 if epiorder==`x' & _merge==3
}

gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)

foreach x in 1 2 3 4{
egen cardio_order`x' = max(cardio`x'), by(extract admidate_f)
}

gen first_cardio = 0
replace first_cardio = 1 if cardio_order1==1
replace first_cardio = 2 if cardio_order2==1 & cardio_order1==0
replace first_cardio = 3 if cardio_order3==1 & cardio_order2==0 & cardio_order1==0
replace first_cardio = 4 if cardio_order4==1 & cardio_order3==0 & cardio_order2==0 & cardio_order1==0

sort extract admidate_f epiorder

gen cons_before_cardio = . if first_cardio==0
replace cons_before_cardio = 0 if first_cardio==1
replace cons_before_cardio = 1 if first_cardio==2
replace cons_before_cardio = 2 if first_cardio==3
replace cons_before_cardio = 3 if first_cardio==4

gen first_pconsult=pconsult[_n-1] if first_cardio==2
replace first_pconsult = pconsult[_n-2] if first_cardio==3
replace first_pconsult = pconsult[_n-3] if first_cardio==4

gen first_tretspef=tretspef[_n-1] if first_cardio==2
replace first_tretspef = tretspef[_n-2] if first_cardio==3
replace first_tretspef = tretspef[_n-3] if first_cardio==4

* Keep the first episode where cardio_order is ==1, with the rest of the info required
gen keep_epi = first_cardio

*replace keep_epi = 1 if first_cardio==0
keep if epiorder==keep_epi


* Drop people who seem to appear multiple times on the same day
bys extract admidate_day admidate_month admidate_year: gen z=_n
egen zz=max(z), by(extract)
drop if zz>1

* Keep the first heart attack if someone has multiple heart attacks
sort extract admidate_day admidate_month admidate_year
egen first_admidate = min(admidate_f), by(extract)
keep if admidate_f == first_admidate



* Now I want to record deaths and readmissions
gen trust_code = substr(procode,1,3)
	
keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac

foreach type in year month day{
rename admidate_`type' ami_admidate_`type'
}

gen readmit_30=0
gen death_30=0

merge 1:m extract_hesid using "$rawdata/ip_`year'", keepusing(admidate_month admidate_day admidate_year dismeth)
	
	drop if _m==2
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	gen ami_date = mdy(ami_admidate_month, ami_admidate_day, ami_admidate_year)
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
		keep extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex ami_date soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* readmit_30 death_30 gpprac
		
gen encrypted_hesid = extract_hesid	
merge 1:m encrypted_hesid using "$rawdata/ip_`z'", keepusing(admidate dismeth)
	

	drop if _m==2
	
	gen admidate_year = substr(admidate,1,4)
	gen admidate_month = substr(admidate,6,2)
	gen admidate_day = substr(admidate,9,2)
	destring admidate_year, replace
	destring admidate_month, replace
	destring admidate_day, replace
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
	keep  extract pconsult procode sitetret trust_code finyear ami ethnos derv_age sex soal soam fulldiag3* tretspef mainspef ami_date readmit_30 death_30 cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac
	compress
	
	save "$savedata/ami_readmit_death_`year'_evercardio", replace
	
}


*2016
****************

forval year==2016(1)2016{
local z = `year'+1

use "$savedata/ami_`year'", clear

gen extract_hesid = encrypted_hesid
gen derv_age = startage	


gen admidate_year = substr(admidate,1,4)
	gen admidate_month = substr(admidate,6,2)
	gen admidate_day = substr(admidate,9,2)
	destring admidate_year, replace
	destring admidate_month, replace
	destring admidate_day, replace
	
	forval x=1(1)9{
gen fulldiag3_`x' = substr(diag_0`x',1,4)
}

forval x=10(1)20{
gen fulldiag3_`x' = substr(diag_`x',1,4)
}


keep encrypted_hesid extract pconsult procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day lsoa msoa fulldiag3* tretspef mainspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* epiorder admimeth gpprac

rename lsoa soal
rename msoa soam

duplicates drop
keep if ami==1
keep if finyear==`year'

merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==2

* We want to keep the first episode where they are treated by a cardiologist - this will be the identify of the consultant we care about
drop if admimeth=="81" | admimeth=="83" | admimeth=="98" | admimeth=="99" 

* Take the first 4 episodes
foreach x in 1 2 3 4{
gen cardio`x' = 0
replace cardio`x' = 1 if epiorder==`x' & _merge==3
}

gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)

/*
bys extract admidate_f epiorder: gen x=_n
keep if x==1
drop x
*/

foreach x in 1 2 3 4{
egen cardio_order`x' = max(cardio`x'), by(extract admidate_f)
}

gen first_cardio = 0
replace first_cardio = 1 if cardio_order1==1
replace first_cardio = 2 if cardio_order2==1 & cardio_order1==0
replace first_cardio = 3 if cardio_order3==1 & cardio_order2==0 & cardio_order1==0
replace first_cardio = 4 if cardio_order4==1 & cardio_order3==0 & cardio_order2==0 & cardio_order1==0

sort extract admidate_f epiorder

gen cons_before_cardio = . if first_cardio==0
replace cons_before_cardio = 0 if first_cardio==1
replace cons_before_cardio = 1 if first_cardio==2
replace cons_before_cardio = 2 if first_cardio==3
replace cons_before_cardio = 3 if first_cardio==4

gen first_pconsult=pconsult[_n-1] if first_cardio==2
replace first_pconsult = pconsult[_n-2] if first_cardio==3
replace first_pconsult = pconsult[_n-3] if first_cardio==4

gen first_tretspef=tretspef[_n-1] if first_cardio==2
replace first_tretspef = tretspef[_n-2] if first_cardio==3
replace first_tretspef = tretspef[_n-3] if first_cardio==4

* Keep the first episode where cardio_order is ==1, with the rest of the info required
gen keep_epi = first_cardio

*replace keep_epi = 1 if first_cardio==0
keep if epiorder==keep_epi


* Drop people who seem to appear multiple times on the same day
bys extract admidate_day admidate_month admidate_year: gen z=_n
egen zz=max(z), by(extract)
drop if zz>1

* Keep the first heart attack if someone has multiple heart attacks
sort extract admidate_day admidate_month admidate_year
egen first_admidate = min(admidate_f), by(extract)
keep if admidate_f == first_admidate


* Now I want to record deaths and readmissions
gen trust_code = substr(procode,1,3)
	
keep encrypted extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac

foreach type in year month day{
rename admidate_`type' ami_admidate_`type'
}

gen readmit_30=0
gen death_30=0

merge 1:m encrypted_hesid using "$rawdata/ip_`year'", keepusing(admidate dismeth)
	
	drop if _m==2
	gen admidate_year = substr(admidate,1,4)
	gen admidate_month = substr(admidate,6,2)
	gen admidate_day = substr(admidate,9,2)
	destring admidate_year, replace
	destring admidate_month, replace
	destring admidate_day, replace
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	gen ami_date = mdy(ami_admidate_month, ami_admidate_day, ami_admidate_year)
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
		keep encrypted extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex ami_date soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* readmit_30 death_30 gpprac

		

merge 1:m encrypted_hesid using "$rawdata/ip_`z'", keepusing(admidate dismeth)
	
	drop if _m==2
	cap drop admidate_year admidate_month admidate_day 
	gen admidate_year = substr(admidate,1,4)
	gen admidate_month = substr(admidate,6,2)
	gen admidate_day = substr(admidate,9,2)
	destring admidate_year, replace
	destring admidate_month, replace
	destring admidate_day, replace
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
	keep  extract pconsult procode sitetret trust_code finyear ami ethnos derv_age sex soal soam fulldiag3* tretspef mainspef ami_date readmit_30 death_30 cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac
	compress
	
	save "$savedata/ami_readmit_death_`year'_evercardio", replace
	
}


*2017
****************

forval year==2017(1)2017{
local z = `year'+1

use "$savedata/ami_`year'", clear

gen extract_hesid = encrypted_hesid
gen derv_age = startage	


gen admidate_year = substr(admidate,1,4)
	gen admidate_month = substr(admidate,6,2)
	gen admidate_day = substr(admidate,9,2)
	destring admidate_year, replace
	destring admidate_month, replace
	destring admidate_day, replace
	
	forval x=1(1)9{
gen fulldiag3_`x' = substr(diag_0`x',1,4)
}

forval x=10(1)20{
gen fulldiag3_`x' = substr(diag_`x',1,4)
}


keep encrypted_hesid extract pconsult procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day lsoa msoa fulldiag3* tretspef mainspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* epiorder admimeth gpprac

rename lsoa soal
rename msoa soam

duplicates drop
keep if ami==1
keep if finyear==`year'

merge m:1 pconsult using "$savedata/cardiologist_list_0517.dta"
drop if _merge==2

* We want to keep the first episode where they are treated by a cardiologist - this will be the identify of the consultant we care about
drop if admimeth=="81" | admimeth=="83" | admimeth=="98" | admimeth=="99" 

* Take the first 4 episodes
foreach x in 1 2 3 4{
gen cardio`x' = 0
replace cardio`x' = 1 if epiorder==`x' & _merge==3
}

gen admidate_f = mdy(admidate_month, admidate_day, admidate_year)

foreach x in 1 2 3 4{
egen cardio_order`x' = max(cardio`x'), by(extract admidate_f)
}

gen first_cardio = 0
replace first_cardio = 1 if cardio_order1==1
replace first_cardio = 2 if cardio_order2==1 & cardio_order1==0
replace first_cardio = 3 if cardio_order3==1 & cardio_order2==0 & cardio_order1==0
replace first_cardio = 4 if cardio_order4==1 & cardio_order3==0 & cardio_order2==0 & cardio_order1==0

sort extract admidate_f epiorder

gen cons_before_cardio = . if first_cardio==0
replace cons_before_cardio = 0 if first_cardio==1
replace cons_before_cardio = 1 if first_cardio==2
replace cons_before_cardio = 2 if first_cardio==3
replace cons_before_cardio = 3 if first_cardio==4

gen first_pconsult=pconsult[_n-1] if first_cardio==2
replace first_pconsult = pconsult[_n-2] if first_cardio==3
replace first_pconsult = pconsult[_n-3] if first_cardio==4

gen first_tretspef=tretspef[_n-1] if first_cardio==2
replace first_tretspef = tretspef[_n-2] if first_cardio==3
replace first_tretspef = tretspef[_n-3] if first_cardio==4

* Keep the first episode where cardio_order is ==1, with the rest of the info required
gen keep_epi = first_cardio

*replace keep_epi = 1 if first_cardio==0
keep if epiorder==keep_epi


* Drop people who seem to appear multiple times on the same day
bys extract admidate_day admidate_month admidate_year: gen z=_n
egen zz=max(z), by(extract)
drop if zz>1

* Keep the first heart attack if someone has multiple heart attacks
sort extract admidate_day admidate_month admidate_year
egen first_admidate = min(admidate_f), by(extract)
keep if admidate_f == first_admidate



* Now I want to record deaths and readmissions
gen trust_code = substr(procode,1,3)
	
keep encrypted extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* gpprac

foreach type in year month day{
rename admidate_`type' ami_admidate_`type'
}

gen readmit_30=0
gen death_30=0

merge 1:m encrypted_hesid using "$rawdata/ip_`year'", keepusing(admidate dismeth)
	
	drop if _m==2
	gen admidate_year = substr(admidate,1,4)
	gen admidate_month = substr(admidate,6,2)
	gen admidate_day = substr(admidate,9,2)
	destring admidate_year, replace
	destring admidate_month, replace
	destring admidate_day, replace
	
	gen admit_date = mdy(admidate_month, admidate_day, admidate_year)
	gen ami_date = mdy(ami_admidate_month, ami_admidate_day, ami_admidate_year)
	gen duration = admit_date - ami_date
	
	gen readmit30 = 0
	replace readmit30 = 1 if duration>0 & duration<31
	
	gen death30=0
	replace death30=1 if dismeth==4 & duration>-1 & duration<31

	drop _merge
	
	egen temp_30 = max(readmit30), by(extract_hesid)
	egen temp2_30 = max(death30), by(extract_hesid)
	
	replace readmit_30=1 if temp_30==1
	replace death_30=1 if temp2_30==1
	
	drop temp_30 temp2_30
	
	* Ordering doesn't matter here as assigned all important variables to all appearances of each patient
	bys extract_hesid: gen x=1 if _n==1
	keep if x==1
	
	* Keep essential variables
		keep encrypted extract pconsult trust_code procode sitetret finyear ami ethnos derv_age sex admidate_year admidate_month admidate_day ami_date soal soam fulldiag3* tretspef mainspef cons_before_cardio first_pconsult first_tretspef opertn_01 opertn_02 opertn_03 opertn_04 opertn_05 opertn_06 *hrg* readmit_30 death_30  gpprac

	save "$savedata/ami_readmit_death_`year'_evercardio", replace
	
}


*****************************
* Merge all years together
*****************************

use "$savedata/ami_readmit_death_2005_evercardio", clear
foreach year in 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017{
append using "$savedata/ami_readmit_death_`year'_evercardio", force
}

replace derv_age = 0 if derv_age>7000
replace derv_age = . if derv_age>120 & derv_age<1000


save "$savedata/ami_readmit_death_evercardio_0517", replace
